﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Threading;
using BaseHelper.Http;
using BaseHelper;
using BaseHelper.Base;
using System.Collections.Concurrent;

namespace DeviceSDK.NetTraffic.Baidu
{
    public class BaiduCache
    {
        private static ConcurrentDictionary<string, DateTime> slUrlTime = new ConcurrentDictionary<string, DateTime>();
        private static ConcurrentDictionary<string, Bitmap> slUrlImage = new ConcurrentDictionary<string, Bitmap>();

        private static object locker = new object();
        private static Thread threadClean;

        public static void Init()
        {
            if (threadClean == null)
            {
                threadClean = new Thread(Clean);
                threadClean.Name = "RT.Helper.Device.Radar.Baidu.BaiduCache.Init().th.threadClean";
                threadClean.Start();
                ThreadHelper.Add(threadClean.ManagedThreadId, threadClean.Name);
            }
                   
        }

        public static void Stop()
        {
            if (threadClean != null && threadClean.IsAlive)
            {
                threadClean.Abort();
            }
            slUrlImage.Clear();
            slUrlTime.Clear();
        }
        public static void Clean()
        {
            int LastTime = DateTime.Now.Hour;

            while(true)
            {

                //10倍的休眠时间
                Thread.Sleep(NetTrafficHelper.Collect_Sleep * 10 * 1000);
                try
                {
                    if (LastTime != DateTime.Now.Hour)
                    {

                        lock (locker)
                        {
                          
                            slUrlTime.Clear();
                            slUrlImage.Clear();
                            LastTime = DateTime.Now.Hour;
                         
                        }

                    }
                    else
                    {
                        lock (locker)
                        {
                            DateTime dt;
                            Bitmap bmp;
                            foreach (string key in slUrlTime.Keys)
                            {
                                if (((TimeSpan)(DateTime.Now - slUrlTime[key])).Seconds > NetTrafficHelper.Collect_Sleep * 50)
                                {
                                    slUrlTime.TryRemove(key,out dt );
                                    slUrlImage.TryRemove(key,out bmp);
                                    bmp = null;
                                }
                                
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Err("DeviceSDK.NetTraffic.Baidu.BaiDuCache.cs", "Clean()", ex);
                }

            }
            //ThreadHelper.Remove(Thread.CurrentThread.ManagedThreadId);
        }

        public static Bitmap GetImage(string Url)
        {
            if (!slUrlTime.ContainsKey(Url) || ((TimeSpan)(DateTime.Now - slUrlTime[Url])).Seconds > (NetTrafficHelper.Collect_Sleep / 2))
            {
                DownNewImage(Url);
            }

            Bitmap bmp;
            if (slUrlImage.ContainsKey(Url) && slUrlImage.TryGetValue(Url,out bmp))
            {
                return bmp;
            }
            return null;       
        
        }


        public static bool DownNewImage(string Url)
        {
            HttpHelperLite http = new HttpHelperLite();

            try
            {
          
                using (Stream imgStream = http.GetStream(Url))
                {
                    if (imgStream != null)
                    {
                       
                        if (slUrlImage.ContainsKey(Url))
                        {
                            slUrlImage[Url] = (Bitmap)Image.FromStream(imgStream);
                            slUrlTime[Url]=DateTime.Now;
                        }
                        else
                        {
                            lock (slUrlImage)
                            {
                                if (slUrlImage.ContainsKey(Url))
                                    return true;
                                slUrlImage[Url] =(Bitmap)Image.FromStream(imgStream);
                                slUrlTime[Url] =DateTime.Now;
                            }
                        }
                        return true;
                    }
                }
            }
            catch(Exception ex)
            {
                //LogHelper.Err("RT.Helper.Device.Radar.Baidu.BaiduCache.cs","DownNewImage()",ex);
            }
            Thread.Sleep(2000);
           return false;
        }
    }
}
